package com.my.helper.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.my.helper.R;
import com.my.helper.bean.Contentlist;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称：BiShe
 * 创建人：
 * 创建时间：2018/5/3 1:27
 */

public class MainAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<Contentlist> datas = new ArrayList<Contentlist>();
    private OnMainClickListener listener;

    public MainAdapter(Context context) {
        this.context = context;
        this.datas = new ArrayList<Contentlist>();
    }

    public void setDatas(List<Contentlist> datas) {
        this.datas = datas;
    }

    public void setListener(OnMainClickListener listener) {
        this.listener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_main, null);
        DailyHolder holder = new DailyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof DailyHolder) {
            ((DailyHolder) holder).tvTitle.setText(datas.get(position).getTitle());
            ((DailyHolder) holder).tvContent.setText(datas.get(position).getIntro());

            if (datas.get(position).getImg().isEmpty()) {
                ((DailyHolder) holder).imageView.setVisibility(View.GONE);
            } else {
                Picasso.with(context).load(datas.get(position).getImg())
                        .networkPolicy(NetworkPolicy.NO_CACHE)
                        .into(((DailyHolder) holder).imageView);
                ((DailyHolder) holder).imageView.setVisibility(View.VISIBLE);
            }

            ((DailyHolder) holder).rlAll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onClickListener(position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class DailyHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private TextView tvTitle;
        private TextView tvContent;
        private RelativeLayout rlAll;

        public DailyHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title_main_item);
            tvContent = itemView.findViewById(R.id.tv_content_main_item);
            imageView = itemView.findViewById(R.id.iv_main_item);
            rlAll = itemView.findViewById(R.id.rl_all_main_item);
        }
    }

    public interface OnMainClickListener {
        void onClickListener(int position);
    }
}
